home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-07 | 10.1 KB | 305 lines |
- /*
- * Java Port Scanner
- *
- * Copyright 2000 Matteo Baccan <mbaccan@planetisa.com>
- * www - http://www.infomedia.it/artic/Baccan
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA (or visit
- * their web site at http://www.gnu.org/).
- *
- */
-
- import java.net.*;
-
- public class portScanner {
-
- /* Public interface ******************************************************/
- static public void main( String argv[] ) {
- String cIPFrom;
- String cPortFrom;
- String cWebTo;
- String cPortTo;
-
- System.out.println( "+---------------------------------------------------------------------------+" );
- System.out.println( "| TCP/IP Port Scanner Version 0.11 04-06-2000 |" );
- System.out.println( "| (C) 2000 by Matteo Baccan http://www.infomedia.it/artic/Baccan |" );
- System.out.println( "+---------------------------------------------------------------------------+" );
-
- try {
- if( argv.length==0 ){
- System.out.println( "Usage: java portScanner [-ip <ip>] [options] " );
- System.out.println( " " );
- System.out.println( "<options>" );
- System.out.println( " -fromip <ip>" );
- System.out.println( " -toip <ip>" );
- System.out.println( " -fromport <nPort>" );
- System.out.println( " -toport <nPort>" );
- System.out.println( " -portfile <cFile>" );
- System.out.println( " -thread <nThread>" );
- System.out.println( " -log" );
- System.out.println( " -timeout <nTimeout>" );
- } else {
- portScanner scanner = new portScanner();
- for( int nParam=0; nParam<argv.length; nParam++ ){
- if( argv[nParam].equalsIgnoreCase("-ip") ){
- if( nParam+1<argv.length ){
- scanner.setIPFrom( argv[nParam+1] );
- scanner.setIPTo( argv[nParam+1] );
- }
- } else if( argv[nParam].equalsIgnoreCase("-fromip") ){
- if( nParam+1<argv.length ){
- scanner.setIPFrom( argv[nParam+1] );
- }
- } else if( argv[nParam].equalsIgnoreCase("-toip") ){
- if( nParam+1<argv.length ){
- scanner.setIPTo( argv[nParam+1] );
- }
- } else if( argv[nParam].equalsIgnoreCase("-fromport") ){
- if( nParam+1<argv.length ){
- scanner.setPortFrom( Integer.parseInt( argv[nParam+1] ) );
- }
- } else if( argv[nParam].equalsIgnoreCase("-toport") ){
- if( nParam+1<argv.length ){
- scanner.setPortTo( Integer.parseInt( argv[nParam+1] ) );
- }
- } else if( argv[nParam].equalsIgnoreCase("-portfile") ){
- if( nParam+1<argv.length ){
- scanner.setPortFile( argv[nParam+1] );
- }
- } else if( argv[nParam].equalsIgnoreCase("-thread") ){
- if( nParam+1<argv.length ){
- scanner.setThread( Integer.parseInt( argv[nParam+1] ) );
- }
- } else if( argv[nParam].equalsIgnoreCase("-log") ){
- scanner.setLog( true );
- } else if( argv[nParam].equalsIgnoreCase("-timeout") ){
- if( nParam+1<argv.length ){
- scanner.setTimeout( Integer.parseInt( argv[nParam+1] ) );
- }
- }
- }
-
- scanner.log("Parameter");
- scanner.log("---------");
- scanner.log("IP from [" +scanner.getIPFrom() +"]");
- scanner.log("IP to [" +scanner.getIPTo() +"]");
- if( scanner.usePortFile() )
- scanner.log("Port file [" +scanner.getPortFile() +"]");
- else {
- scanner.log("Port from [" +scanner.getPortFrom() +"]");
- scanner.log("Port to [" +scanner.getPortTo() +"]");
- }
- scanner.log("Thread [" +scanner.getThread() +"]");
- scanner.log("Log [" +scanner.getLog() +"]");
- scanner.log("Timeout [" +scanner.getTimeout() +"]");
- scanner.log("" );
- scanner.run();
- }
-
- } catch(Exception e) {
- System.out.println( e.toString() );
- }
- }
- /*************************************************************************/
-
- public portScanner(){}
-
- private byte[] cIPFrom = {127,0,0,1};
- public void setIPFrom( String cIP ){
- try {
- cIPFrom = InetAddress.getByName( cIP ).getAddress();
- } catch ( Throwable e ){
- System.out.println( e.toString() );
- }
- }
- public String getIPFrom(){
- return ipToString( cIPFrom );
- }
-
- private String ipToString( byte []cIP ){
- return (cIP[0]&0xFF)+ "."+
- (cIP[1]&0xFF)+ "."+
- (cIP[2]&0xFF)+ "."+
- (cIP[3]&0xFF);
- }
-
- private byte[] cIPTo = {127,0,0,1};
- public void setIPTo( String cIP ){
- try {
- cIPTo = InetAddress.getByName( cIP ).getAddress();
- } catch ( Throwable e ){
- System.out.println( e.toString() );
- }
- }
- public String getIPTo(){
- return ipToString( cIPTo );
- }
-
- private int nPortFrom = 1;
- public void setPortFrom( int nPort ){
- nPortFrom = nPort;
- }
- public int getPortFrom(){
- return nPortFrom;
- }
-
- private int nPortTo = 1024;
- public void setPortTo( int nPort ){
- nPortTo = nPort;
- }
- public int getPortTo(){
- return nPortTo;
- }
-
-
- private String cPortFile = "";
- public void setPortFile( String cFile ){
- cPortFile = cFile;
- }
- public String getPortFile(){
- return cPortFile;
- }
-
- public void setThread( int nThread ){
- nMaxThread = nThread;
- }
- public int getThread(){
- return nMaxThread;
- }
-
- private boolean bLog=false;
- public void setLog( boolean bLog ){
- this.bLog = bLog;
- }
- public boolean getLog(){
- return bLog;
- }
-
- public void log( int nMsg ){
- log( new Integer( nMsg ).toString() );
- }
- public void log( String cMsg ){
- System.out.println( cMsg );
- }
-
- private int nTimeout = 2000;
- public void setTimeout( int nTime ){
- nTimeout = nTime;
- }
- public int getTimeout(){
- return nTimeout;
- }
-
- public boolean usePortFile(){
- return (cPortFile.length()>0);
- }
-
- public void run() {
- port portToScan = new port();
- if( usePortFile() ) {
- portToScan.setFile( cPortFile );
- } else {
- portToScan.setRange( nPortFrom, nPortTo );
- }
-
- long nMils = System.currentTimeMillis();
- Thread ip;
- while( true ){
-
- if( cIPFrom[0]>=cIPTo[0] &&
- cIPFrom[1]>=cIPTo[1] &&
- cIPFrom[2]>=cIPTo[2] &&
- cIPFrom[3]>=cIPTo[3] ){
- if( !(cIPFrom[0]==cIPTo[0] &&
- cIPFrom[1]==cIPTo[1] &&
- cIPFrom[2]==cIPTo[2] &&
- cIPFrom[3]==cIPTo[3] ) ){
- break; // End of thread
- }
- }
-
- for( int nLocalPort=0; nLocalPort< portToScan.length(); nLocalPort++ ){
- if( !isMaxThread() ){
- threadAdd();
- ip = new scanSingleIP( this,
- ipToString(cIPFrom),
- portToScan.getPort(nLocalPort),
- portToScan.getDesc(nLocalPort),
- bLog,
- nTimeout );
- ip.start();
- } else {
- try {
- Thread.sleep( 500 );
- } catch ( Throwable e ){}
- }
- }
-
- if( cIPFrom[3]<255 ){
- cIPFrom[3]++;
- } else {
- cIPFrom[3]=0;
- if( cIPFrom[2]<255 ){
- cIPFrom[2]++;
- } else {
- cIPFrom[2]=0;
- if( cIPFrom[1]<255 ){
- cIPFrom[1]++;
- } else {
- cIPFrom[1]=0;
- if( cIPFrom[0]<255 ){
- cIPFrom[0]++;
- } else {
- break; // No more address
- }
- }
- }
- }
- }
-
- while( isActiveThread() ){
- try {
- Thread.sleep(500 );
- } catch ( Throwable e ){}
- }
-
- nMils = (System.currentTimeMillis()-nMils)/1000;
- log( "Seconds : " +nMils );
- }
-
- //------------------------------------------------------------------------//
- // Thread Manager //
- //------------------------------------------------------------------------//
- private int nThread = 0;
- private int nMaxThread = 50;
- private int nPortTotal = 0;
- synchronized boolean isMaxThread() {
- return (nThread>=nMaxThread);
-
- }
- synchronized boolean isActiveThread() {
- return (nThread>0);
-
- }
- public synchronized void threadAdd() {
- nPortTotal++;
- nThread++;
- }
- public synchronized void threadDel() {
- nThread--;
- }
- //--------------------------------------------------------------------------
- }
-